package com.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.demo.model.MovieTicketing;
import com.demo.dao.MovieTicketingDao;
import com.demo.service.MovieTicketingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杨森
 * @since 2022-04-06
 */
@Service
public class MovieTicketingServiceImpl extends ServiceImpl<MovieTicketingDao, MovieTicketing> implements MovieTicketingService {
    @Autowired
    private RedisTemplate<String, Object> ort;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addMovieTicketings(List<MovieTicketing> movieTicketings) {
        boolean b = saveBatch(movieTicketings);
        //遍历获取场次id
        HashSet<Integer>hashSet=new HashSet<>();
        for (MovieTicketing movieTicketing : movieTicketings) {
            hashSet.add(movieTicketing.getNumberOfSessionsId());
        }
        Iterator it = hashSet.iterator();
        while(it.hasNext()){
            Integer next =(Integer) it.next();
            QueryWrapper<MovieTicketing>queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("Number_of_sessions_id",next);
            List<MovieTicketing> movieTicketings1 = baseMapper.selectList(queryWrapper);
            for (MovieTicketing movieTicketing : movieTicketings1) {
                ort.opsForHash().put("numberOfSessionsId:"+next.toString(),"ticketingId"+movieTicketing.getTicketingId().toString(),"0");
            }
        }
        return b;
    }
}
